<?php
class Sections extends MY_Controller
{
	var $curl = 'sections';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $submenu = "_menu";
			
	function __construct()
	{
		parent::__construct();
				
		$this->load->model(array('/public/sections_model'));

		$this->validation->set_fields($this->sections_model->fields);

		$this->data['Title'] = T_('Sections');

		$this->language = parent::getLang();

		$this->name = T_('Sections');
	}

	function index()
	{
        $this->sections_model->search();
     	$this->renderLayout();
	}
	
	function create()
	{
		//constant values
		$allSections = $this->sections_model->getAllSections();
		$section_options = array();
		foreach($allSections as $row) {
			$section_options[$row['section_id']] = $row['section_name'];
		}
		
		$this->data['section_options'] = $section_options; 
		$this->data['showHideClass'] = 'hide_tag';
		$this->data['type_options'] = array(
											'0' => $this->sections_model->fields['section'],
											'1' => $this->sections_model->fields['subsection']
											);
		if (!empty($_POST)) {
			
			$rules = array('description','name');
			
			$this->validation->set_rules($this->sections_model->getRules($rules));
			
			if ($this->validation->run() === TRUE)
			{				
				$item['section_name']		= $this->validation->name; 
				$item['section_descr']		= $this->validation->description;
				
				$type						= $this->validation->type;
				if($type != 0) {
					$item['section_sub'] = $this->validation->section;
				} else {
					$item['section_sub'] = '0';
				}
				
				if ($this->sections_model->insert($item))
       			{	
					redirect($this->curl . '/index'); 
       			}				
			}
		}
		
		$this->data['Title'] .= ' / ' . T_('New section');
		
		$this->renderLayout();			
	}
	
	function deleteItems()
		{
	        $data = $this->sections_model->delete($_REQUEST['items']);
	        echo $data;
		}
	
	function delete($id)
	 {
	    $data = $this->sections_model->delete($id);
	   	redirect($this->curl . '/index');
	 }
	
	function edit($id = null)
	{
		if (!empty($id)) {
			
			$object = $this->sections_model->get($id);
			
			//constant values
			$allSections = $this->sections_model->getAllSections();
			foreach($allSections as $row) {
				$section_options[$row['section_id']] = $row['section_name'];
			}
			
			$this->data['section_options'] = $section_options; 
			$this->data['type_options'] = array(
												'0' => $this->sections_model->fields['section'],
												'1' => $this->sections_model->fields['subsection']
												);
			if (!count($_POST)) {
				
				$this->validation->id				= $object['section_id'];
				$this->validation->name				= $object['section_name'];
				$this->validation->description		= $object['section_descr'];

				$sub = $object['section_sub']; 
				if($sub==0){
					$this->validation->type		= 0;
					$this->data['showHideClass'] = 'hide_tag';
				} else {
					$this->validation->type		= 1;
					$parent = $this->sections_model->get($sub);
					$this->validation->section	= $parent['section_id'];
					$this->data['showHideClass'] = '';
				} 
			}else {				
				$rules = array('description','name');
			
				$this->validation->set_rules($this->sections_model->getRules($rules));
				
				if ($this->validation->run() === TRUE) {
					
					$item['section_id']				= $this->validation->id;
					$item['section_name']			= $this->validation->name;
					$item['section_descr']			= $this->validation->description; 
					if($this->validation->type == 0) {
						$item['section_sub']			= $this->validation->type;
					} else {
						$item['section_sub']			= $this->validation->section;
					}
					
					if ($this->sections_model->update($item)){
					      	redirect($this->curl . '/index');
					   } 
	       			}				
				}
			}	
			
		$this->data['Title'] .= ' / ' . T_('Edit');
		
		$this->renderLayout();
	}
	 
	 
	function loadSectionsBySectionId($id_sections = null, $wrap_elem = '', $onchange = '')
	{
		$this->layout = 'ajax';
		$this->data['id_sections'] = $id_sections;
		$this->data['wrap_elem']  = $wrap_elem;

		if (!$onchange) 
		{
			$onchange = '';
		}
		
		if ($wrap_elem)
		{
			$onchange = 'onchange="'.$onchange.'"';
		}

		$this->data['onchange']  = $onchange;
		$this->load->view('sections/ajax/select_sections', $this->data);
	}
}
?>